package com.example.service;


import com.example.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Scanner;

@Service
public class ServerService {


    @Autowired
    private WebSocketServer webSocketServer;

    @PostConstruct
    public void init() {

        // 创建一个线程处理键盘输入
        Thread inputThread = new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            while (true) {
                System.out.print("请输入要发送的消息：");
                String message = scanner.nextLine();
                // 在这里将消息发送给客户端
                webSocketServer.sendToAllClient(message);
            }
        });
        inputThread.start(); // 启动输入线程
    }
}